package cate.game.role.farm;

import cate.common.table.d.GDDailyFb;
import cate.common.table.d.GDFunc;
import cate.game.activity.mooncake.MooncakeActivity;
import cate.game.activity.mtowerchallenge.MTowerChallengeActivity;
import cate.game.activity.worldboss.RoleWorldBoss;
import cate.game.role.Role;
import easy.java.practice.errorcode.EcResult;

public class PlayTimesHandler {
    public EcResult<PlayTimes> buyTimes(Role role, int funcId, int num) {
        EcResult<PlayTimes> r = new EcResult<>();
        if (num < 1) {
            return r.fail("错误的购买次数");
        }
        // 本次需要购买的次数 默认是买1次，如果前端传参数则以前端参数为准
        PlayTimes playTimes = getPlayTimes(role, funcId);
        if (playTimes == null) {
            return r.fail("无法购买挑战次数");
        }
        for (int i = 1; i <= num; i++) {
            EcResult<Void> buyRes = playTimes.checkBuy(role, funcId);
            if (!buyRes.ok()) {
                return r.fail(buyRes.message);
            }
        }
        r.data = playTimes;
        return r;
    }

    private PlayTimes getPlayTimes(Role role, int funcId) {
        PlayTimes playTimes = null;
        switch (funcId) {
            case GDFunc.TOWER:
                playTimes = role.getFarm().tower.mopTimes;
                break;
            case GDFunc.GuildFunc.BOSS:
                playTimes = role.getFarm().guildBoss.playTimes;
                break;
            case GDFunc.FORBIDDEN_ZONE:
                playTimes = role.getFarm().forbidden.playTimes;
                break;
            case GDFunc.LADDER_RACE:
                playTimes = role.getPvp().ladder.playTimes;
                break;
            case GDFunc.GuildFunc.FAIRYLAND:
                playTimes = role.getFarm().guildFairyland.playTimes;
                break;
            case GDFunc.WORLD_BOSS:
                RoleWorldBoss roleWorldBoss = role.getActivity().getEnableActivitySingle(GDFunc.WORLD_BOSS);
                if (roleWorldBoss != null) {
                    playTimes = roleWorldBoss.playTimes;
                }
                break;
            case GDFunc.CROSS_BOSS_FARM:
                playTimes = role.getFarm().crossBoss.playTimes;
                break;
            case GDFunc.GRADE_PVP:
                playTimes = role.getPvp().gradePvp.playTimes;
                break;
            case GDFunc.THRONE_TRIAL:
                playTimes = role.getPvp().throne.trial.playTimes;
                break;
            case GDFunc.MOONCAKE:
                MooncakeActivity single = role.getActivity().getEnableActivitySingle(GDFunc.MOONCAKE);
                playTimes = single.challenge.playTimes;
                break;
            default:
                if (funcId >= GDFunc.DAILY_FB_FIRST && funcId <= GDFunc.DAILY_FB_END)
                    playTimes = role.getFarm().dailyFb.fbList.get(GDDailyFb.Type.toType(funcId)).playTimes;
                break;
        }
        return playTimes;
    }
}
